<%@page contentType="text/html;charset=utf-8" pageEncoding="UTF-8" %>
<%@page import="org.ipo.mm.web.util.pager.AbstractPager, org.ipo.mm.web.util.ConstantManager, java.util.Collection, org.ipo.mm.web.util.security.SecurityManager, java.util.Vector" %>
<%@taglib prefix="l" uri="http://struts.apache.org/tags-logic" %>
<%@taglib prefix="b" uri="http://struts.apache.org/tags-bean" %>
<%@taglib prefix="h" uri="http://struts.apache.org/tags-html"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<script type="text/javascript">
    function start(){
        startInstitution();
    }

    function startInstitution(){
        switchMenu('searchPanelInstitution');
    <l:iterate id="inst" name="INST_SEARCH_RESULTS" scope="request">
            switchMenu('i${inst.id}');
    </l:iterate>
        }

</script>

<h1><b:message key="menu_institutions" /></h1>
<%
            Collection<Integer> privileges = (Collection<Integer>) request.getSession().getAttribute(ConstantManager.SESSION_USER_PRIVILEGES);
            if (privileges.contains(SecurityManager.CREATE_INSTITUTION_PRIVILEGE)) {%>

<h:link styleClass="createNew" action="/protected/dispatch?method=showCreateInstForm"><b:message key="inst_create"/></h:link>

<%}%>



<!--advanced search form -->
<a class="detailSearch" onclick="switchMenu('searchPanelInstitution');" href="#"><b:message key="gen_advanced_search"/></a>
<div id="searchPanelInstitution">
    <br>
    <h:form  styleId="form3" action="/protected/institution_search">
        <div style="clear:none">
            <div class="detailSearchForm" >
                <em><strong><b:message key="gen_search_institution_label"/></strong></em><br />
                <table border="0" cellspacing="0" cellpadding="0">
                    <tr>
                        <td><b:message key="inst_name"/></td>
                        <td>
                            <h:text property="nameFilter" styleClass="filter" />
                            <h:select property="nameOperator">
                                <h:option value="AND">AND</h:option>
                                <h:option value="OR">OR</h:option>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="inst_address"/></td>
                        <td>
                            <h:text property="addressFilter" styleClass="filter" />
                            <h:select property="addressOperator">
                                <h:option value="AND">AND</h:option>
                                <h:option value="OR">OR</h:option>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="inst_email"/></td>
                        <td>
                            <h:text property="emailFilter" styleClass="filter" />
                            <h:select property="emailOperator">
                                <h:option value="AND">AND</h:option>
                                <h:option value="OR">OR</h:option>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="inst_phone"/></td>
                        <td>
                            <h:text property="telephoneFilter" styleClass="filter" />
                            <h:select property="telephoneOperator">
                                <h:option value="AND">AND</h:option>
                                <h:option value="OR">OR</h:option>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="inst_fax"/></td>
                        <td>
                            <h:text property="faxFilter" styleClass="filter" />
                            <h:select property="faxOperator">
                                <h:option value="AND">AND</h:option>
                                <h:option value="OR">OR</h:option>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="inst_representatives"/></td>
                        <td>
                            <h:text property="representativeFilter" styleClass="filter" />
                            <h:select property="representativeOperator">
                                <h:option value="AND">AND</h:option>
                                <h:option value="OR">OR</h:option>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="repr_inst_type"/></td>
                        <td>
                            <h:select property="typeFilter" size="5" multiple="true" styleClass="list" >
                                <l:iterate id="type" name="search_types">
                                    <h:option value="${type.id}">${type.name}</h:option>
                                </l:iterate>
                            </h:select>

                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="repr_inst_region"/></td>
                        <td>
                            <h:select property="regionFilter" size="5" multiple="true" styleClass="list" >
                                <l:iterate id="region" name="search_regions">
                                    <h:option value="${region.id}">${region.name}</h:option>
                                </l:iterate>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="repr_inst_family"/></td>
                        <td>
                            <h:select property="familyFilter" size="5" multiple="true" styleClass="list" >

                                <l:iterate id="family" name="search_families">
                                    <h:option value="${family.id}">${family.name}</h:option>
                                </l:iterate>
                            </h:select>
                        </td>
                    </tr>
                    <tr>
                        <td><b:message key="repr_inst_city"/></td>
                        <td>
                            <h:select property="cityFilter" size="5" multiple="true" styleClass="list" >

                                <l:iterate id="city" name="search_cities">
                                    <h:option value="${city.id}">${city.name}</h:option>
                                </l:iterate>
                            </h:select>
                        </td>

                    </tr>

                </table>
            </div><!--detailSearchForm-->
        </div>
        <span style="clear:left"><b:message key="gen_search_order_label"/> <h:select property="orderBy">
                <h:option value="1"><b:message key="inst_name"/></h:option>
                <h:option value="2"><b:message key="inst_email"/></h:option>
                <h:option value="3"><b:message key="inst_address"/></h:option>
                <h:option value="4"><b:message key="inst_phone"/></h:option>
                <h:option value="5"><b:message key="inst_fax"/></h:option>
            </h:select> <h:submit><b:message key="gen_search"/></h:submit></span>
    </h:form>

</div><!--searchPanel-->

<div class="tabelaNova">

    <!--<div class="item">-->
    <table>
        <tr>            
            <th scope="col" nowrap="true"><h:link action="/protected/institution_search?method=showInstitutions&clearStack=true&orderBy=1"><b:message key="inst_name"/></h:link></th>
            <th scope="col"><h:link action="/protected/institution_search?method=showInstitutions&clearStack=true&orderBy=6"><b:message key="inst_type"/></h:link></th>
            <th scope="col"><h:link action="/protected/institution_search?method=showInstitutions&clearStack=true&orderBy=7"><b:message key="inst_family"/></h:link></th>
            <th scope="col"><h:link action="/protected/institution_search?method=showInstitutions&clearStack=true&orderBy=8"><b:message key="inst_region"/></h:link></th>
            <th scope="col"><h:link action="/protected/institution_search?method=showInstitutions&clearStack=true&orderBy=9"><b:message key="inst_city"/></h:link></th>
            <th scope="col"><b:message key="meetings_meetings"/></th> 
        </tr>
        <l:empty name="INST_SEARCH_RESULTS">
            <tr class="red1" >
                <td colspan="6" nowrap="true">
                    <div class="centeredinfo">
                        <img src="<%= request.getContextPath()%>/icons/info.png" align="top"/>
                        <b:message key="gen_no_search_results"/>

                    </div>
                </td>
            </tr>

        </l:empty>
        <l:iterate id="inst" name="INST_SEARCH_RESULTS" scope="request">

            <tr class="red1">                
                <td><a onclick="switchMenu('i${inst.id}');" class="expand">
                        <span style="font-weight: bolder">
                            <%--<h:link paramName="inst" paramId="iid" paramProperty="id" action="/protected/representative_search">--%>
                                <b:write name="inst" property="name"/>
                            <%--</h:link>--%>

                        </span>
                    </a>
                </td>
                <td>${inst.type.name}</td>
                <td>${inst.family.name}</td>
                <td>${inst.region.name}</td>
                <td>${inst.city.name}</td>
                <td><h:link action="/protected/meeting_search" paramId="iid" paramName="inst" paramProperty="id"><b:message key="inst_find_meetings"/></h:link></td>               
            </tr>
            <tr>
                <td colspan="8" class="red2">


                    <div class="itemDetail" id="i${inst.id}">
                        <strong><b:message key="gen_action"/></strong><br><br>
                        <span style="border-style:solid;padding:5px;margin-top: 15px; margin-bottom: 15px; border-color: orange">
                            <%
            if (privileges.contains(SecurityManager.EDIT_INSTITUTION_PRIVILEGE)) {%>
                            <%-- security measures to tight
                                            <c:choose>
                                                <c:when test="${USER == inst.user || USER.type == USER_TYPE_ADMIN_PA}">
                                                    <h:link styleClass="details" action="/protected/dispatch?method=showUpdateInstForm"
                                                            paramId="iid" paramName="inst" paramProperty="id"><b:message key="gen_update"/></h:link>
                                                </c:when>
                                                <c:otherwise>
                                                    <b:message key="gen_noaction"/>
                                                </c:otherwise>
                                            </c:choose>
                            --%>
                            <h:link styleClass="details" action="/protected/dispatch?method=showUpdateInstForm"
                                    paramId="iid" paramName="inst" paramProperty="id"><b:message key="gen_update"/></h:link>

                            <%            } else {

                            %>

                            <b:message key="gen_noaction"/>
                            <%}%>


                            <%

            if (privileges.contains(SecurityManager.DELETE_INSTITUTION_PRIVILEGE)) {%>

                            <h:link styleClass="delete" action="/protected/dispatch?method=deleteInstitution"
                                    paramId="iid" paramName="inst" paramProperty="id" onclick="return confirm('Are you sure you want to delete institution?/Da li ste sigurni da zelite da izbrisete ovu instituciju?')"
                                    ><b:message key="gen_delete"/></h:link>

                            <%}%>
                        </span><br>

                        <br/>

                        <b:message key="inst_address"/> : ${inst.address}<br />
                        <b:message key="inst_email"/> : ${inst.email}<br />
                        <b:message key="inst_phone"/> : ${inst.telephone}<br />
                        <b:message key="inst_fax"/> : ${inst.fax}<br />
                        <b:message key="inst_url"/> : <a href="${inst.web}" target="_blank">${inst.web}</a><br />
                        <b:message key="gen_note"/> : ${inst.note}<br />
                        <p><strong><b:message key="inst_representatives"/> :</strong><br />
                            <l:iterate id="rep" name="inst" property="representativeCollection">
                                <c:if test="${rep.deleted == false}">

                                    <c:choose>
                                        <c:when test="${rep.user == USER}">
                                            <h:link action="/protected/dispatch?method=showUpdateReprForm"
                                                    paramId="rid" paramName="rep" paramProperty="id">
                                                <b:write name="rep" property="firstName"/>
                                                <b:write name="rep" property="lastName"/>
                                            </h:link>
                                        </c:when>

                                        <c:otherwise>
                                            <b:write name="rep" property="firstName"/>
                                            <b:write name="rep" property="lastName"/>
                                        </c:otherwise>
                                    </c:choose>

                                    <b:write name="rep" property="email"/>
                                    <b:write name="rep" property="telephone"/>
                                    <b:write name="rep" property="fax"/>
                                    <b:write name="rep" property="function"/>
                                    <h:link action="/protected/meeting_search" paramId="rid" paramName="rep" paramProperty="id"><b:message key="inst_find_meetings"/></h:link><br />
                                </c:if>

                            </l:iterate>
                        </p>
                        <p>
                            <strong><b:message key="gen_action"/></strong><br><br>
                            <%--
                            <span style="border-style:solid;padding:5px;margin-top::15px;">

                                <%
            if (privileges.contains(SecurityManager.EDIT_INSTITUTION_PRIVILEGE)) {%>
                                <c:if test="${USER == inst.user || USER.type == USER_TYPE_ADMIN_PA}">
                                    <h:link styleClass="details" action="/protected/dispatch?method=showUpdateInstForm"
                                            paramId="iid" paramName="inst" paramProperty="id"><b:message key="gen_update"/></h:link>
                                </c:if>


                                <%}%>



                                <%

            if (privileges.contains(SecurityManager.DELETE_INSTITUTION_PRIVILEGE)) {%>

                                <h:link styleClass="delete" action="/protected/dispatch?method=deleteInstitution"
                                        paramId="iid" paramName="inst" paramProperty="id" onclick="return confirm('Are you sure you want to delete institution?/Da li ste sigurni da zelite da izbrisete ovu instituciju?')"
                                        ><b:message key="gen_delete"/></h:link>

                                <%}%>
                            </span>
                            --%>
                        </p>
                        <!--</div>--><!--itemDetail-->
                </td>
            </tr>
        </l:iterate>
        <tr>
            <th scope="col"><h:link action="/protected/institution_search?method=showInstitutions&clearStack=true&orderBy=1"><b:message key="inst_name"/></h:link></th>
            <th scope="col"><h:link action="/protected/institution_search?method=showInstitutions&clearStack=true&orderBy=6"><b:message key="inst_type"/></h:link></th>
            <th scope="col"><h:link action="/protected/institution_search?method=showInstitutions&clearStack=true&orderBy=7"><b:message key="inst_family"/></h:link></th>
            <th scope="col"><h:link action="/protected/institution_search?method=showInstitutions&clearStack=true&orderBy=8"><b:message key="inst_region"/></h:link></th>
            <th scope="col"><h:link action="/protected/institution_search?method=showInstitutions&clearStack=true&orderBy=9"><b:message key="inst_city"/></h:link></th>
            <th scope="col"><b:message key="meetings_meetings"/></th>
        </tr>
    </table>
</div><!--item-->

</div><!--tabela-->


<div id="navigation">

    <% AbstractPager pager = (AbstractPager) request.getSession().getAttribute(ConstantManager.SESSION_INST_PAGER);%>

    <c:if test="${INST_SEARCH_SHOW_PREV}">

        <a href="<%= request.getContextPath() + "/protected/institution_search.do?method=showMeetings&p=" + (pager.getPageNumber() - 1)%>" class="previous"><b:message key="gen_previous"/></a>

    </c:if>


    <%-- padajuca lista sa brojevima strana --%>


    <select name="URL" onchange="window.location=this.options[selectedIndex].value">
        <%

            Vector<Integer> pagenumbers = (Vector<Integer>) request.getAttribute("INST_SEARCH_PAGENUMBERS");
            if (pagenumbers != null) { // is quicksearch is used then there is no paging
                for (int i = 0; i < pagenumbers.size(); i++) {
                    int pageParam = pager.getPageNumber();
                    int pageNumber = pagenumbers.get(i);

                    if (pageParam == pageNumber) {
                        out.write("<option value=\"" + request.getContextPath() + "/protected/institution_search.do?method=showInstitutions&p=" + pageNumber + "\" selected=\"selected\">" + pageNumber + "</option>");
                    } else {
                        out.write("<option value=\"" + request.getContextPath() + "/protected/institution_search.do?method=showInstitutions&p=" + pageNumber + "\">" + pageNumber + "</option>");

                    }


                }
            }

        %>


    </select>


    <%-- end padajuca lista sa brojvima strana --%>


    <c:if test="${INST_SEARCH_SHOW_NEXT}">

        <a href="<%= request.getContextPath() + "/protected/institution_search.do?method=showMeetings&p=" + (pager.getPageNumber() + 1)%>" class="next"><b:message key="gen_next"/></a>
    </c:if>

        <p>
            <b:message key="gen_items_showing"/> <%= pager.getItemInterval() %> <b:message key="gen_items_of_total"/> <%= pager.getResultsTotal() %>
        </p>

</div>
